$(document).ready(function(){
	// 加载侧边栏
	// 更新侧边栏时间
	tasks_updateTime();
	// 加载日历模块
	Calendar.init();
});

/*更新侧边栏时间*/
function tasks_updateTime() {
	var today = new Date();
	$("#mainAside_time").html(today.Format("YYYY-MM-dd hh:mm:ss"));
	setTimeout("tasks_updateTime()",1000);
}

/*日历功能模块*/
var Calendar = {
	// 初始化
	init : function() {
		var dateObj = new Date();
		
		this.year = dateObj.getFullYear();
		this.month = dateObj.getMonth() + 1;
		this.date = dateObj.getDate();
		this.isLeapYear = dateObj.isLeapYear();
		this.show(this.year,this.month,this.isLeapYear);
	},
	// 加载
	show : function(year,month,isLeapYear) {
		// 初始化
		$("#calendar .active").removeClass("active");
		$("#calendar .disabled").removeClass("disabled");
		$("#calendar tbody tr").css("display","table-row")

		// 更改年月
		var text = "<span class='year'>" + year + "</span>年<span class='month'>" + month + "</span>月";
		$("#calendar caption > span").html(text);

		// 加载该月所有日期
		var totalDays = this.getTotalDays(month,isLeapYear);
		var firstDay = new Date(year,month-1,1);
		var firstNo = (firstDay.getDay() === 0 ? firstDay.getDay() + 6 : firstDay.getDay() - 1 );
		var date = firstNo;

		for (var i = 0; i < totalDays; i++) {
			$($("#calendar tbody td")[date]).html(i+1);
			if (year === this.year && month === this.month && (i+1) === this.date) {
				$($("#calendar tbody td")[date]).addClass("active");
			}
			date++;
		}

		// 加载上个月和下个月的日期（补充空的日期）
		var lastMonth = (month === 1 ? 12 : month - 1);
		var lastMonth_year = (lastMonth === 12 ? year - 1 : year);
		var lastMonth_totalDays = this.getTotalDays(lastMonth,new Date(lastMonth_year,lastMonth,1).isLeapYear());

		for (var i = firstNo-1; i >= 0; i--) {
			$($("#calendar tbody td")[i]).html(lastMonth_totalDays).addClass("disabled");
			lastMonth_totalDays--;
		}

		var nextMonth = (month === 12 ? 1 : month + 1);
		var nextMonth_year = (nextMonth === 1 ? year + 1 : year);
		var nextMonth_totalDays = this.getTotalDays(nextMonth,new Date(nextMonth_year,nextMonth,1).isLeapYear());
		var nextMonth_date = 1;

		for (var i = date; i < $("#calendar tbody td").length; i++) {
			$($("#calendar tbody td")[i]).html(nextMonth_date).addClass("disabled");
			nextMonth_date++;
		}

		// 删除空白行
		for (var i = 0; i < $("#calendar tbody tr").length; i++) {
			var tr = $("#calendar tbody tr")[i];
			if ($(tr).children("td:first-child").hasClass("disabled") && $(tr).children("td:last-child").hasClass("disabled")) {
				$(tr).css("display","none");
			}
		}

		this.change();
	},
	// 获取该月的总天数
	getTotalDays : function(month,isLeapYear) {
		if (month === 1 || month === 3 || month === 5 || month === 7 || month === 8 || month === 10 || month === 12) {
			return 31;
		} else if (month === 4 || month === 6 || month === 9 || month === 11) {
			return 30;
		} else {
			if (isLeapYear) {
				return 29;
			} else {
				return 28;
			}
		}
	},
	// 点击上个月和下个月事件
	change : function() {
		var that = this;

		$("body").off("click","#calendar caption a");

		$("body").on("click", "#calendar caption a", function() {
			var thisMonth = Number($("#calendar caption .month").html());
			var thisYear = Number($("#calendar caption .year").html());

			if ($(this).hasClass("l")) {
				var month = (thisMonth === 1 ? 12 : thisMonth - 1);
				var year = (month === 12 ? thisYear - 1 : thisYear);
			} else if ($(this).hasClass("r")) {
				var month = (thisMonth === 12 ? 1 : thisMonth + 1);
				var year = (month === 1 ? thisYear + 1 : thisYear);
			}

			that.show(year, month, new Date(year, month, 1).isLeapYear());
			return false;
		});
	}
}